<!doctype html>
<html>

<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<link rel="stylesheet" href="../res/css/weui.css" />
	<link rel="stylesheet" href="../res/css/weuix.css" />
	<!-- <link rel="stylesheet" href="../res/css/common.css" /> -->
	<script src="../res/js/tim/trtc.js"></script>
	<script src="../res/js/zepto.min.js"></script>
	<script src="../res/js/zepto.weui.js"></script>
	<script src="../res/js/common.js?v=1"></script>
	<script src="../res/js/consultationWebsocket.js"></script>
	<script src="consult_chat.js?v=13"></script>

	<script src="../res/js/tim/purl.js"></script>
	<script src="../res/js/tim/vconsole.min.js"></script>
	<script src="../res/js/tim/GenerateTestUserSig.js"></script>
	<script src="../res/js/tim/lib-generate-test-usersig.min.js"></script>
	<script src="../res/js/tim/AgoraRTC_N-4.17.2.js"></script>
	<script src="../res/js/tim/swTRTCConsult.js"></script>
	<script src="../res/js/tim/imBase.js"></script>
	<script>
		var log_id, isOnline, onlineNum = 0,
			endOpen, ossdata = [],
			Picdata, inquirypatientId, user_id, textList = "";
		var patientIM = getQst("patientIM"),
			oldDoctorName;
		var fileArr = [];
		var pop = 0; //判断是否popup过
		var first = 1; //判断是否第一次加载历史信息
		var tvflag = 0,
			truename = '',
			patientImg = '',
			doctorHeadImg = '';
		var Num = 0,
			score = 5;
		var ConditionListName = [];
		var ConditionName = '',
			isData = 0,
			patientIdcard = 1;
		var timeOutEvent = null;
		var fromMyDoctor = getQst("fromMyDoctor")
		var FeeInfomation = null; //付费信息
		var finishAsk = false;  //问诊结束
		var moveFlag = 0;	//滑动记录
		let firstMsg;
		let IOSterminal;

		let reShowFlag = ''		//弹窗打开记录
		$(function () {

            getHistoryMsg()
			// 是IOS终端
			IOSterminal = /(iPhone|iPhone OS|Mac OS)/i.test(navigator.userAgent);
			//隐藏弹窗
			$(".darkBackgroud").hide();
			$(".popubBox1").hide();

			// 判断是否TV端
			var tvflag = getQst("tvflag");
			if (tvflag == 1) {
				$('#videoDiv2').css({
					"display": "none"
				})
				$('#videoDiv1').attr({
					class: "bvideo"
				});
			} else {
				//什么都不做
			}

			log_id = getQst("logId"); //logid问诊号
			log_type = getQst("log_type"); //问诊类型
			doctor_id = getQst("doctor_id"); //医生id
			console.log(doctor_id);
			doctor_imid = getQst("doctor_imid"); //医生im
			inquirypatientId = getQst("inquirypatientId"); //真正的问诊号（？
			patientIM = getQst("patientIM") //患者IM


			$(".btn11").on("click", function () {
				TRTCTYPE === 'Tencent' ? createClient() : RTCjoin()
				$(".darkBackgroud").hide();
				$(".popubBox1").hide();
			})

			calChatHeight();

			//获取oss参数
			newOssPolicy()
			// console.log();

			// 上传图片
			$('#btnUpPic').on("click", function () {
				if (pop == 0) {
					pop++;
					window.removeEventListener("popstate", out, false);
					$('#upLoadimg').popup() //打开
					setTimeout(function () {
						window.addEventListener("popstate", out, false);
					}, 300);
				} else if (pop > 0) {
					pop = 0;
					window.removeEventListener("popstate", out, false);
					$.closePopup(); //关闭
					setTimeout(function () {
						window.addEventListener("popstate", out, false);
					}, 300);
				}
			});

			//发送消息
			$('#btnSend').on("click", function () {
				// onSendMsg();
				if (finishAsk) {
					msgInfo('问诊已结束');
					//清空输入框
					$("#send_msg_text").val('');
				} else {
					var onemsg = '';
					var content = $("#send_msg_text").val();
					textList = $("#send_msg_text").val();
					if (content == '') {
						msgInfo('请先输入内容再发送');
						return;
					}
					let msgData = {
						content,
						packageType: 4,
						contentType: 2,
						roomId: log_id,
						timestamp: new Date().getTime()
					}
					parent.consultationSocket.send(4, msgData);
					//清空输入框
					$("#send_msg_text").val('').focus();
				}
			});

			// 唤起键盘时 滚动到聊天记录底部
			$("#send_msg_text").on('focus', e => {
				scrollBottom()
			})

			$(".close-popup_photo").on("click", function () {
				$.closePopup(); //关闭
			})

			$('body').on('touchmove', '.slidebg', function () {
				$(document.body).on('touchmove.prevUC', function (e) {
					e.preventDefault();
				});
			});
			$('body').on('touchend', '.slidebg', function () {
				$(document.body).off('touchmove.prevUC');
			});
			$('.weui-dialog__btn_primary').on('click', function () {
				Refee()
			})
			$('.weui-dialog__btn_default').on('click', function () {
				$('#androidDialog1').fadeOut(200);
			})
			$('.backIndex').on('click', function () {
				goPageNo(-1)
			})

			// 点击其他地方收起撤回按钮
			$('body').on('touchstart', function () {
				$('.delete_btn').hide()
			})
			// 点击遮罩层关闭大图
			$(".close_img").on('touch', function () {
				$('.img_mask').removeClass('weui-mask--visible')
				$('.img_dialog').removeClass('weui-dialog--visible')
			})

			// document.addEventListener('contextmenu', e => {
			// 	e.preventDefault()
			// 	return false
			// })
			$("body").on('contextmenu', function (e) {
				e.preventDefault()
			})

			$("#dataAnimalHeat").on('input', inputLimit(2))
			$("#dataRr").on('input', inputLimit(0))
			$("#dataSbp").on('input', inputLimit(0))
			$("#dataDbp").on('input', inputLimit(0))
			$("#dataHeartRate").on('input', inputLimit(0))
			$("#dataGlu").on('input', inputLimit(2))
		});

		function inputLimit(limit) {	//限制输入小数点后位数
			let reg =
				limit ? new RegExp(`\\d+(\\.\\d{0,${limit}})?`)
					: new RegExp('\\d+')
			return function (e) {
				let $this = $(this),
					$val = $this.val(),
					validata = reg.exec($val)

				$this.val(validata ? validata[0] : '')
			}
		}

		function scrollBottom(){
			let list = document.getElementById('chat_Log')
			if (list.lastChild) {
				let timmer = setTimeout(() => {
					list.lastChild.scrollIntoView()
					clearTimeout(timmer)
				}, 100);
			}
		}

		window.onload = function () {
			/*禁止ios缩放，双击和双指*/
			document.addEventListener('touchstart', function (event) {
				if (event.touches.length > 1) {
					event.preventDefault();
				}
			});
			var lastTouchEnd = 0;
			document.addEventListener('touchend', function (event) {
				var now = (new Date()).getTime();
				if (now - lastTouchEnd <= 300) {
					event.preventDefault();
				}
				lastTouchEnd = now;
			}, false);
			document.addEventListener('gesturestart', function (event) {
				event.preventDefault();
			});
		}

		function newOssPolicy() {
			newAjaxPost("/h5/oss/ossPolicy", {}, newOssPolicyOk);
		}

		function newOssPolicyOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				ossdata = jsonInfo.data
			}
		}
		// getSuffix() 方法用于获取文件名的后缀
		var getSuffix = function (fileName) {
			var pos = fileName.lastIndexOf(".");
			var suffix = '';
			if (pos != -1) {
				suffix = fileName.substring(pos);
			}
			return suffix;
		};
		//上传图片
		function newUpdateUserHeadImg(data) {
			// 在聊天室中显示
			parent.consultationSocket.send(4, {
				content: data.filename,
				packageType: 4,
				contentType: 1,
				roomId: log_id,
				timestamp: new Date().getTime()
			});
			pop = 0;
			window.removeEventListener("popstate", out, false);
			$.closePopup(); //关闭
			setTimeout(function () {
				window.addEventListener("popstate", out, false);
				$("#uploaderFiles").html('');
				$(".weui-uploader__input-box").show()
				Picdata = '';
			}, 300);
		}


		// ws监听
		parent.consultationSocket.onMessage = function (res) {
			console.log(res, '会诊消息数据chat');
			switch (res.code) {
				case 1:
					sessionStorage.setItem('cTencentConfig',  jsonToStr({
						room: res.roomId + '',
						...(res.configuration || {})
					}))
					break;
				case 3: // 会诊开始
					sessionStorage.setItem('cTencentConfig',  jsonToStr({
						room: res.roomId + '',
						...(res.configuration || {})
					}))
					$(".darkBackgroud").show();
					$(".popubBox1").show();
					// handleConsultationStart(res);
					break;
				case 6: // 医生离开
					handleConsultationEnd(res);
					break;
				case 4:
					if (res.msgType === 4) { // 消息撤回信息
                      let msg = {
                        msgUserName: res.sender,
                        msgText: res.content,
                        msgDate: formatDate(new Date(res.timestamp)),
                        msgType: res.contentType,
                        userId: res.userId
                      }
					}
					break;
				default:
					// 其他消息类型
					break;
			}
		}

		//医生掉线时，退费
		function Refee() {
			var params = {
				"doctorId": doctor_id,
				"isFrozen": 1
			}
			newAjaxPost("/h5/wxpay/refund", params, RefeeOk);
		}

		function RefeeOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				msgInfo('退费成功，请重新选择医生问诊');
				var isIos = checkWxAndIos();
				if (isIos && getQst("log_type") == '1') {
					goPage('inquiry_skip.htm')
				} else {
					goPage("inquiry_doctor_index.htm");
				}
			}
		}
		//收到分诊时，退费
		function newRefee() {
			var params = {
				"doctorId": doctor_id,
				"isFrozen": 1
			}
			newAjaxPost("/h5/wxpay/refund", params, newRefeeOk);
		}

		function newRefeeOk(jsonInfo) {
			if (jsonInfo.code == 1) {
				// msgInfo('退费成功，请重新选择医生问诊');
			}
		}

	</script>
	<style>
		html {
			touch-action: none;
			touch-action: pan-y;
		}

		body{
			display: flex;
			flex-direction: column;
			height: 100vh;
			overflow: auto;
		}

		#divChatMain{
			overflow: auto;
			flex-grow: 1;
		}

		.onemsgUser{
			user-select: none;
			-webkit-user-select: none;
		}

		.weui-cells {
			font-size: 14px;
		}

		.header {
			text-align: center;
			z-index: 1;
			width: 100%;
			top: 0;
			transition: 0.5s;
		}

		.input_msg {
			width: 95%;
			display: inline-block;
			height: 25px;
			border: 0.5px solid #d8d8d8;
			line-height: 25px;
			border-radius: 4px;
			padding: 2.5px;
			text-align: left;
		}

		.weui-form-li {
			vertical-align: middle;
			position: relative;
			margin: 0 0px 0 5px
		}

		.weui-form-text {
			display: inline-block;
			position: relative;
			margin-top: 0px;
			margin-right: 0px;
			cursor: pointer;
			font-size: 14px
		}

		.weui-icon-radio {
			-webkit-border-radius: 100%;
			border-radius: 100%;
			width: 11px;
			height: 11px;
			display: inline-block;
			margin-top: 4px;
			margin-right: 0px;
			border: 1px solid #b8b8b8;
			background-color: #fafafa;
			vertical-align: top;
		}

		.font14 {
			font-size: 14px;
			line-height: 20px
		}

		.mright15 {
			margin-right: 15px
		}

		.msgbody pre {
			white-space: pre-wrap
		}

		.onemsgUser .msgbody img{
			width: 100%;
			max-height: 400px;
			vertical-align: middle;
			-webkit-touch-callout: none;
			/* pointer-events: none; */
		}

		.chat_box{
			overflow: auto;
			width: 100vw;
		}

		.chat_box div p {
			font-size: 14px;
			line-height: 20px
		}

		.height150 {
			height: 150px
		}

		.svideo {
			display: flex;
			flex-wrap: wrap;
		}
		.svideo .remote-player-wrapper {
			flex: 1;
		}

		.svideo video {
			object-fit: fill
		}

		.bvideo video {
			width: 100%;
			height: 100%;
			object-fit: fill
		}

		.width33 {
			width: 33.333%
		}

		.footer {
			background-color: White
		}

		.footer .bottom-tab{
			display: flex;
			justify-content: flex-start;
		}

		.weui-uploader__input-box {
			margin-top: 30px;
		}

		.weui-icon-delete .weui-icon_gallery-delete {
			color: #FFFFFF;
			font-size: 36px;
			line-height: 81px;
		}

		.weui-gallery__del {
			display: block;
			padding-bottom: 18px;
		}

		.ending {
			margin: 10%;
			margin-bottom: 70%;
			width: 80%;
			border-radius: 5px;
		}

		.toolbar {
			background: #ffffff;
			box-shadow: #999 0 0 5px -2px;
		}

		.weui-btn:after {
			content: none;
		}

		.btn_end {
			background-color: #ffffff;
			color: #0066FF;
			font-size: 20px;
		}

		.endtitle {
			position: absolute;
			display: block;
			width: 100%;
			padding: 0;
			font-size: .85rem;
			font-weight: normal;
			line-height: 2.2rem;
			color: #3d4145;
			text-align: center;
			white-space: nowrap;
		}

		.nav {
			width: 100%;
		}

		.nav li {
			height: 30px;
			width: 20%;
			list-style: none;
			float: left;
		}

		.nav li a {
			width: 24px;
			position: absolute;
			text-decoration: none;
			display: inline-block;
			text-indent: -99999px;
			background: url(../res/images/star-off-big.png) no-repeat;
			z-index: 10;
		}

		.star a {
			background: url(../res/images/star-on-big.png) no-repeat !important;
		}

		.weui-header {
			padding: 0 0;
		}

		.fontRed {
			color: red;
		}

		.recordList>div {
			background: white;
			margin: 10px;
			border-radius: 5px;
		}

		.weui-btn_primary {
			background-color: #1189ff;
		}

		.weui-tabbar__item.weui-bar__item--on .weui-tabbar__label {
			color: #1189ff;
		}

		.weui-cell {
			padding: 4px 10px;
		}

		.weui-tabbar__item {
			padding: 0;
		}

		.weui-tabbar {
			background-color: #ffffff;
		}

		.weui-tabbar:before {
			border-top: 0px;
		}

		.expertlist_map {
			border-radius: 5px;
			margin: 10px;
			box-shadow: 0px 0px 3px #888888;
		}

		.expertlist_map_top {
			display: flex;
			padding: 10px;
		}

		.expertlist_map_top>div.doctor_info {
			flex: 4;
			padding-left: 8px;
		}

		.expertlist_map_img {
			text-align: center;
			color: #f9a821;
		}

		.expertlist_map_img.color999 {
			color: #999;
		}

		.expertlist_map_img img {
			width: 58px;
			height: 58px;
			border-radius: 50%;
			display: block;
		}

		.doctor_name {
			font-size: 16px;
			font-weight: 500;
			padding-bottom: 3px;
			line-height: 27px
		}

		.doctor_name span:first-child {
			background-color: #ECF6FF;
			color: #1189FF;
			width: 60px;
			height: 19px;
			border-radius: 5px;
			font-size: 13px;
			padding: 2px 5px;
			margin: 0 5px;
		}

		.doctor_name span:last-child {
			background-color: #FEF8EE;
			color: #ECA02C;
			width: 60px;
			height: 19px;
			border-radius: 5px;
			font-size: 13px;
			padding: 2px 5px;
		}

		.age {
			font-size: 14px;
			color: #666;
			margin: 3px 0;
		}

		#start {
			color: black;
		}

		#start span {
			color: #1189ff;
		}

		.img_dialog {
			max-width: unset;
			width: 100%;
			top: 50%;
		}

		.show {
			width: 100%;
		}

		.weui-mask_transparent {
			z-index: 3000
		}

		.showChat {
			padding: 2px;
			border: 1px solid #1189ff;
			border-radius: 5px;
			background-color: #ECF6FF;
		}

		#dateWrite .weui-cell__hd {
			width: 24%;
			height: 40px;
			line-height: 40px;
			font-size: 14px;
		}

		#dateWrite .weui-cell__bd {
			width: 73%;
			height: 40px;
			line-height: 40px;
			font-size: 14px;
		}

		.weui-input {
			width: 77%;
		}

		.inline {
			display: inline-block;
		}

		.line {
			border-bottom: 1px solid #E0E0E0;
		}

		.line:last-child {
			border-bottom: 0px;
		}

		.dataShow .line {
			padding: 5px 0;
		}

		.dataShow .weui-cell__hd {
			min-width: 144px;
		}

		.select_img {
			width: 20px;
			top: 10px;
			position: absolute;
		}

		.delete_btn {
			display: none;
			position: absolute;
			/* bottom: 0px; */
			left: 40%;
			top: 1px;
			padding: 8px 12px;
			background-color: rgba(255, 255, 255, .7);
			border-radius: 6px;
			line-height: 1;
			white-space: nowrap;
			font-size: 14px;
			border: 1px solid #e0e0e0;
		}

		.weui-dialog__btn {
			font-size: 14px;
		}

		#androidDialog1 .weui-mask {
			z-index: 2000;
		}


		/* 弹出框样式 */
		.popubBox {
			width: 70%;
			display: none;
			background-color: #fff;
			position: fixed;
			top: 250px;
			left: 15%;
			z-index: 100;
			border-radius: 10px;
			z-index: 10000;
		}

		.popubBox1 {
			width: 70%;
			display: none;
			background-color: #fff;
			position: fixed;
			top: 250px;
			left: 15%;
			z-index: 1000;
			border-radius: 10px;
			z-index: 10000;
		}

		.title {
			width: 100%;
			height: 40px;
			line-height: 40px;
			text-align: center;
			font-family: PingFang SC;
			font-weight: bold;
			color: #353535;
			font-size: 20px;
			margin: 10px;
		}

		.titleMessage {
			width: 90%;
			margin: 30px 5%;
			font-size: 16px;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
			text-align: center;
		}

		.btnList {
			width: 90%;
			margin: 15px 5%;
			display: flex;
		}
		.btn11 {
			width: 95%;
			margin: 0 2.5%;
			font-size: 14px;
			font-family: PingFang SC;
			font-weight: 500;
			color: #1189FF;
			background-color: #fff;
			text-align: center;
			line-height: 40px;
			height: 40px;
			border: 2px solid #03A6FF;
			border-radius: 20px;
		}

		.my-require::before{
			content: "*";
			color: red;
			padding-right: 2px;
		}
		.a-piece{
			margin: 10px 0;
			box-shadow: #999 0 0 5px -2px;
			flex-direction: column;
			align-items: flex-start;
		}
		.a-piece > div{
			padding-bottom: 8px;
		}
		.a-piece > div:last-child{
			padding-bottom: 0;
		}
		.score-row{
			display: flex;
			flex-direction: column;
			align-items: flex-start;
		}
		hr{
			width: 100%;
		}

		.tell-us-something textarea{
			width: 228px;
			padding: 0 2px;
			line-height: 20px;
			min-height: 20px;
			max-height: 73px;
			min-width: 180px;
			max-width: 279px;
			border-radius: 4px;
		}

		.notRead::after{
			padding-top: 15px;
			content: '未读';
			color: red;
		}
		.isRead::after{
			padding-top: 15px;
			content: '已读';
			color: #393939;
		}
		.box-end{
			padding: 15px;
			box-shadow: #8885 0 0 4px 0px;
		}

		.onlineRecipe{
			display: none;
		}

		.msgImg{
			max-width: 100%;
		}
	</style>

<body ontouchstart>
	<div class="weui-header" id="divCameraMic" style="width:100%;background-color: black; flex-shrink: 0;">
		<div id="videoDiv2" class="bvideo" style="height: 180px"></div>
		<div id="videoDiv1" class="svideo"></div>
	</div>
	<div id="divChatMain">
		<div id="divChatLog">
			<div id="chat_Log" class="chat_box">
			</div>
		</div>
	</div>
	<div class="footer">
		<div class="weui-cell">
			<div class="weui-cell__bd"><input id="send_msg_text" class="input_msg" type="text" placeholder="请输入文字再发送" />
			</div>
			<div class="weui-cell__ft"><button id="btnSend" class="weui-btn weui-btn_mini weui-btn_primary">发送</button>
			</div>
		</div>
		<!-- 导航 -->

		<div class="bottom-tab">
			<a href="#" class="weui-tabbar__item" id="btnUpPic" style="width: 100px;flex: initial">
				<img src="../res/images/inquiry_chat02.png" style="width: 38px;" />
				<p class="weui-tabbar__label">上传图片</p>
			</a>
		</div>
	</div>
	<!--上传图片-->
	<div id="upLoadimg" class='weui-popup__container'>
		<div class="weui-popup__overlay"></div>
		<div class="weui-popup__modal">
			<div class="weui-cells weui-cells_form picform" style="overflow:auto">
				<div class="toolbar">
					<div class="toolbar-inner" style="    margin: 5px;">
						<!--<a href="javascript:;" class="picker-button close-popup">确定</a>-->
						<div class="weui-cell__bd font14"></div>
						<div class="weui-cell__ft"><a href="javascript:;"
								class="weui-btn weui-btn_mini weui-btn_primary close-popup_photo">关闭</a></div>
					</div>
				</div>

				<div class="weui-gallery" id="gallery">
					<span class="weui-gallery__img" id="galleryImg"></span>
				</div>
				<div class="weui-cells weui-cells_form">
					<div class="weui-cell">
						<div class="weui-cell__bd">
							<div class="weui-uploader">
								<div class="weui-uploader__hd">
									<p class="weui-uploader__title">图片上传(最多一张)：</p>
								</div>
								<div class="weui-uploader__bd" style="margin-bottom: 0;margin-right: 0;">
									<ul class="weui-uploader__files" id="uploaderFiles">
									</ul>
								</div>
								<div class="weui-uploader__input-box">
									<input id="uploaderInput" class="weui-uploader__input zjxfjs_file" type="file"
										accept="image/*" multiple="" />
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="weui-cell weui-flex">
				<div class="weui-flex__item middle"><a href="javascript:;" onclick="saveImg()" id="save"
						class="weui-btn weui-btn_mini weui-btn_primary">确认上传</a></div>
			</div>
		</div>
	</div>
	<!-- 点击放大图片 -->
	<div class="weui-mask img_mask close_img" style="opacity: 1;"></div>
	<div class="weui-dialog img_dialog close_img" style="opacity: 1;">
		<img class="show" src="">
	</div>
	<div class="popubBox1">
		<div class="titleMessage">本次会诊为视频会诊,需要开启摄像头(麦克风)</div>
		<div class="btnList">
			<div class="btn11">
				确定
			</div>
		</div>
	</div>
</body>

</html>
